package com.wiscamp.ninechapters.problems.math;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 算术运算不等式
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class LatexArithmeticInequality extends LatexText {
    public LatexArithmeticInequality(String expression) {
        this.textValue = expression.trim();
        this.latexType = LatexTextEnum.ARITHMETIC_OPERATIONS_INEQUALITY;
    }

    /**
     * Remove the brace in the expression
     *
     * @param expression arithmetic expression
     * @param leftBrace  left brace symbol
     * @param rightBrace right brace symbol
     * @return expression without the brace
     */
    private String removeBrace(String expression, String leftBrace, String rightBrace) {
        // 暂时不考虑括号嵌套, 算术表达式只比对数字不比对运算符，可以直接去除括号
        var tempText = expression.replace(" ", "").trim();
        tempText = tempText.replace(leftBrace, "");
        tempText = tempText.replace(rightBrace, "");
        return tempText;
    }

    /**
     * Get terms in the expression
     *
     * @return term list
     */
    @Override
    public Collection<String> toTerms() {
        String tempText = textValue;
        if (tempText.contains("(") || tempText.contains(")")) {
            // 表达式包含括号的情况，去括号
            tempText = removeBrace(tempText, "(", ")");
        }

        // 6 × 2 ÷ 5 × 2 ÷ 7
        tempText = tempText.replace(" ", "");
        tempText = tempText.replace("+", ",");
        tempText = tempText.replace("-", ",");
        tempText = tempText.replace("×", ",");
        tempText = tempText.replace("÷", ",");

        return Arrays.stream(tempText.split(",")).collect(Collectors.toList());
    }
}
